/*订单审批页面，查看分配数和已使用数以及门店所在的城市的总剩余量*/
public class ActivityOrderAllocationDetailController
{
     public String ActivityId
       {
           get
           {
               return System.currentPageReference().getParameters().get('ActivityId');  
           }
           set;
       }
        public String OutletId
       {
           get
           {
               return System.currentPageReference().getParameters().get('OutletId');  
           }
           set;
       }
       
    public List<AllocationResourceEntry> ResourceList{get;set;}//当前门店的信息
    public List<AllocationResourceEntry> CityRemainderList{get;set;}//城市剩余量
    public string CityName{get;set;}//门店所在城市名称
    public Integer CityTotalResourceNumber{get;set;}//城市总资源数（个）
    
    public ActivityOrderAllocationDetailController()
    {
        if(ResourceList==NULL)
            ResourceList=new List<AllocationResourceEntry>();
        else
            ResourceList.clear();
            
        if(CityRemainderList==null)
            CityRemainderList=new List<AllocationResourceEntry>();
        else
            CityRemainderList.clear();
            
        map<string,integer> outletDistributorMap=getDistributorData(false,null);
     
        //查询分配量、已使用量
        for(Resource_Allocation_Detail__c rs:[select Resource_Allocation__r.Product_Name__r.Name,
                                                                ProductId__c,
                                                                Resource_Quantity_Number__c,
                                                                HasUsedResourceNumber__c 
                                                                from Resource_Allocation_Detail__c 
                                                                where ActivityId__c=:ActivityId 
                                                                and Participator_Detail_Outlet__r.Outlet_Name__c=:OutletId])
         {
             
             AllocationResourceEntry entry=new AllocationResourceEntry();
             entry.ETName=rs.Resource_Allocation__r.Product_Name__r.Name;
             entry.HasBeenAllocatedResourceNumber=Integer.valueOf(rs.HasUsedResourceNumber__c);
             entry.TotalNumber=Integer.valueOf(rs.Resource_Quantity_Number__c);
             if(outletDistributorMap.containsKey(rs.ProductId__c))
                 entry.RealPlaceOrderNumber=outletDistributorMap.get(rs.ProductId__c);//实际下单数
             else
                 entry.RealPlaceOrderNumber=0;
            
            entry.RemainderOfResource=entry.TotalNumber-entry.RealPlaceOrderNumber;//剩余资源数
                 
             ResourceList.add(entry);
         }  
         
         //获取门店所在城市的名称
         Account acc=[select id,CityName__c from Account where Id=:OutletId limit 1];  
         map<string,integer> allDistributorMap=getDistributorData(true,acc.CityName__c);
         if(acc!=NULL)
         {  
            
             for(AggregateResult  rs:[select sum(Resource_Quantity_Number__c) totalNumber,
                                                  Resource_Allocation__r.Product_Name__r.Name,
                                                  Resource_Allocation__r.Product_Name__c
                                                  from Resource_Allocation_Detail__c 
                                                  where ActivityId__c=:ActivityId 
                                                  and Participator_Detail_Outlet__r.City__c=:acc.CityName__c
                                                  group by Resource_Allocation__r.Product_Name__r.Name,Resource_Allocation__r.Product_Name__c])
             {
                 Integer num=0;
                 if(allDistributorMap.get((string)rs.get('Product_Name__c'))!=NULL)
                 {
                     num=allDistributorMap.get((string)rs.get('Product_Name__c'));
                 }
                 
                 AllocationResourceEntry entry=new AllocationResourceEntry();
                 entry.CityName=acc.CityName__c;
                 entry.ETName=(string)rs.get('Name');
                 if(Integer.valueOf(rs.get('totalNumber')) > num)
                     entry.RemainderOfResource=Integer.valueOf(rs.get('totalNumber'))-num;
                 else
                     entry.RemainderOfResource=0;
                     
                 CityRemainderList.add(entry);         
              //   entry.TotalNumber=Integer.valueOf(rs.Resource_Quantity_Number__c);
                 //CityRemainderList
             }
         }
    }
    //获取经销商的实发数
    //参数：是否按城市查询
    public map<string,integer> getDistributorData(boolean isQueryByCity,string cityName)
    {
        string sql='select DistributorSetOrderNumber__c,Product_Name__c,Order_Name__r.Outlet_name__c from Order_Item__c '+
                         ' where Order_Name__r.Activity_Information__c=\''+ActivityId+'\'';
        if(isQueryByCity)
            sql+=' and  Order_Name__r.Outlet_name__r.CityName__c=\''+cityName+'\'';
         else
             sql+=' and Order_Name__r.Outlet_name__c=\''+OutletId+'\'';   
         map<string,integer> outletDistributorMap=new map<string,integer>();
         map<string,integer> allDistributorMap=new map<string,integer>();
         for(Order_Item__c oItem:Database.query(sql))
         {
              if(allDistributorMap.containsKey(oItem.Product_Name__c))
              {
                  Integer disNum=0;
                  if(oItem.DistributorSetOrderNumber__c!=NULL)
                      disNum=Integer.valueOf(oItem.DistributorSetOrderNumber__c);
                  if(allDistributorMap.get(oItem.Product_Name__c)!=NULL)
                  {
                      Integer num=allDistributorMap.get(oItem.Product_Name__c);
                       num+=disNum;
                       allDistributorMap.put(oItem.Product_Name__c,num);
                  }
                 
              }
              else
              {
                  allDistributorMap.put(oItem.Product_Name__c,Integer.valueOf(oItem.DistributorSetOrderNumber__c));
              }
         }
      //   if(isQueryByCity)
             return allDistributorMap;
    //     else
      //       return outletDistributorMap;
    }
}